package com.system;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author: clm
 * @Date: 2018-10-27
 */

public class FileUtils {
    public static boolean bitmapSaveToFile(Bitmap bitmap, String filename){
        String dir = GLApplication.getContext().getExternalCacheDir().getAbsolutePath();
        File file = new File(dir, filename);
        if(file.exists()){
            file.delete();
        }
        try {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    public static Bitmap getFromAssetsAtBitmap(String fileName){
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(GLApplication.getContext().getResources().getAssets().open(fileName));
        } catch (IOException e) {
            e.printStackTrace();
            GLLog.loge("加载位图异常:" + e.getMessage());
            return null;
        }
        return bitmap;
    }
    public static String getFromAssets(String fileName) throws IOException {
        InputStreamReader inputReader = new InputStreamReader( GLApplication.getContext().getResources().getAssets().open(fileName),"UTF-8");

        BufferedReader bufReader = new BufferedReader(inputReader);
        String line="";
        String Result="";
        while((line = bufReader.readLine()) != null)
            Result += line + "\n";
        return Result;
    }
    public static InputStreamReader getFromAssetsInputStreamReader(String fileName) throws IOException {
        InputStreamReader inputReader = new InputStreamReader( GLApplication.getContext().getResources().getAssets().open(fileName),"UTF-8");
        return inputReader;
    }
}
